using System.Collections.Generic;
using System.Linq;

namespace ProjectEuler.Core
{
    public class Problem52 : IProjectEulerProblem
    {
        protected int GetSmallestNum()
        {
            foreach (var num in GetNumbers())
            {
                var nums = new List<int> { num};
                nums.Add(num*2);
                nums.Add(num*3);
                nums.Add(num*4);
                nums.Add(num*5);
                nums.Add(num*6);

                var length = num.ToString().Length;
                if (nums.All(n => n.ToString().Length == length))
                {
                    if(HaveSameDigits(nums))
                    {
                        return num;
                    }
                }
            }
            return -1;
        }

        private static bool HaveSameDigits(IEnumerable<int> nums)
        {
            var nummatch = OrderedDigits(nums.First());
            return nums.All(n => OrderedDigits(n) == nummatch);
        }

        private static string OrderedDigits(int num)
        {
            return string.Join("",num.ToString().OrderBy(c => c));
        }

        protected IEnumerable<int> GetNumbers()
        {
            for (var i = 1; i > 0; i++)
            {
                yield return i;
            }
            yield break;
        }

        public int Number
        {
            get { return 52; }
        }

        public string Description
        {
            get { return "Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits."; }
        }

        public string Answer
        {
            get { return GetSmallestNum().ToString(); }
        }
    }
}